/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.SignatureException;
import java.security.cert.CRLException;
import java.security.cert.CertificateException;
import java.security.cert.X509CRL;
import java.security.spec.InvalidKeySpecException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.CA_Service;
import model.CertificateDAO;
import model.FilePathDAO;
import model.KeyIO;
import sun.security.x509.X509CertImpl;

/**
 *
 * @author XuanTuan
 */
public class RevokeCertControl extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, NoSuchAlgorithmException, InvalidKeySpecException, CRLException, NoSuchProviderException, SecurityException, SignatureException, InvalidKeyException, CertificateException {
        response.setContentType("text/html;charset=UTF-8");

        //update in CRL
        String certPath = request.getParameter("certPath");
        PrivateKey privateKey = KeyIO.getPrivateKey(new FileInputStream(getServletContext().getRealPath("\\CAKeys") + "\\pri"));
        X509CertImpl certImpl = new X509CertImpl(CA_Service.getCert(new FileInputStream(certPath)));
        X509CRL x509crl = null;
        try {
            FileInputStream fileInputStream = new FileInputStream(getServletContext().getRealPath("\\CRL") + "\\crl.crl");
            x509crl = CA_Service.revokeCert(CA_Service.getCRL(fileInputStream, privateKey), certImpl, privateKey);
        } catch (Exception ex) {
            x509crl = CA_Service.revokeCert(null, certImpl, privateKey);
        }

        //write new crl to file
        FileOutputStream fileOutputStream = new FileOutputStream(getServletContext().getRealPath("\\CRL") + "\\crl.crl");
        fileOutputStream.write(x509crl.getEncoded());
        fileOutputStream.close();

        //update database
        CertificateDAO.revokeCert(certImpl.getSerialNumber() + "");
        FilePathDAO.saveFilePath("crl", getServletContext().getRealPath("\\CRL\\crl.crl"));

        response.sendRedirect("./ManagerCert.jsp");
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            try {
                processRequest(request, response);
            } catch (CertificateException ex) {
                Logger.getLogger(RevokeCertControl.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(RevokeCertControl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeySpecException ex) {
            Logger.getLogger(RevokeCertControl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (CRLException ex) {
            Logger.getLogger(RevokeCertControl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchProviderException ex) {
            Logger.getLogger(RevokeCertControl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(RevokeCertControl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SignatureException ex) {
            Logger.getLogger(RevokeCertControl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(RevokeCertControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            try {
                processRequest(request, response);
            } catch (CertificateException ex) {
                Logger.getLogger(RevokeCertControl.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(RevokeCertControl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeySpecException ex) {
            Logger.getLogger(RevokeCertControl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (CRLException ex) {
            Logger.getLogger(RevokeCertControl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchProviderException ex) {
            Logger.getLogger(RevokeCertControl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(RevokeCertControl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SignatureException ex) {
            Logger.getLogger(RevokeCertControl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(RevokeCertControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
